package com.lintf.devdemo.error;

import com.lintf.devdemo.common.Result;
import com.lintf.devdemo.common.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/11/04-13:58
 * @ package_name:com.lintf.devdemo.error
 * @ project_name:DevDemo
 * @ desc:全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = BusinessException.class)
    public Result bizExceptionHandler(BusinessException e) {
        log.error("e.getErrorCode() === === ===>>>" + e.getErrorCode().toString());
        log.error("e.getErrorMsg() === === ===>>>" + e.getErrorMsg());
        return Result.defineError(e);
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(Exception e) {
        log.error(e.getMessage(), e);
        return Result.otherError(ResultCode.FAILED);
    }
}
